﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace zichan.BLL
{
    public class RoleService
    {
        public static string RoleAdd(Role model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.Roles.Count(o => o.Name == model.Name) > 0)
                    return "存在相同名称";
                db.Roles.Add(model);
                db.SaveChanges();
                return string.Empty;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string RoleEdit(Role model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.Roles.Count(o => o.Id != model.Id && o.Name == model.Name) > 0)
                    return "存在相同名称";
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return string.Empty;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string RoleRemove(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.StaffRoles.Count(o => o.RoleId == id) > 0)
                    return "本角色已对职员授权，请先删除对应职员授权";
                Role m = db.Roles.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    db.Roles.Remove(m);
                    db.SaveChanges();
                    return string.Empty;
                }
                return "角色不存在";
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Role Get(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.Roles.FirstOrDefault(o => o.Id == Id);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static List<Role> List()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.Roles.OrderBy(o => o.Id).ToList();

            }
        }

        /// <summary>
        /// 用户授权
        /// </summary>
        /// <returns></returns>
        public static int Authorize(int rid, Staff staff)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                int id;
                var s = db.Staffs.FirstOrDefault(o => o.Name == staff.Name);
                if (s == null)
                {
                    db.Staffs.Add(staff);
                    db.SaveChanges();
                }
                id = s.Id;

                if (db.StaffRoles.Count(o => o.RoleId == rid && o.StaffId == id) > 0)
                    return 0;

                StaffRole sf = new StaffRole();
                sf.StaffId = id;
                sf.RoleId = rid;
                db.StaffRoles.Add(sf);
                db.SaveChanges();
                return 1;
            }
        }

    }
}
